<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        {{ count }}
        <my-cpn @add="changeCount" :count='count' @del="changeCount"></my-cpn>
    </div>
    <script src="../js/vue.js"></script>
    <template id="myCpn">
        <div>
            <button @click="add">+1</button>
            <button @click="del">-1</button>
        </div>
    </template>
    <script>
        const app = new Vue({
            el:'#app',
            data(){
                return {
                    count:0
                }
            },
            components:{
                'my-cpn':{
                    template:'#myCpn',
                    data(){
                        return {

                        }
                    },
                    props:['count'],
                    methods:{
                        add(){
                            // props拿到的数据不可改变
                            // 所以我们需要一个新的值去接收它
                            let newCount = this.count
                            newCount++
                            // 改变了这个值，我们是不是应该把它告诉父组件
                            this.$emit('add',newCount)
                        },
                        del(){
                            let newCount = this.count
                            newCount--
                            this.$emit('del',newCount)
                        }
                    }
                }
            },
            methods:{
                changeCount(val){
                    this.count = val
                }
            }
        })
    </script>
</body>
</html>